﻿using CBUMachineDataManage.MachineData;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;

namespace NDK.Module.StationLogic
{
    public enum STATUSCODE
    {
        运行中 = 0,
        待机中 = 1,
        报警中 = 9
    }

    public class SunWayMes
    {
        #region 属性
        /// <summary>
        /// 车间
        /// </summary>
        public string WorkshopName { get; set; }

        /// <summary>
        /// 料号
        /// </summary>
        public string ProductName { get; set; }

        /// <summary>
        /// 工单
        /// </summary>
        public string MOName { get; set; }

        /// <summary>
        /// 机台
        /// </summary>
        public string Station { get; set; }

        /// <summary>
        /// 线体
        /// </summary>
        public string LineNumber { get; set; }

        /// <summary>
        /// 批次条码
        /// </summary>
        public string SN { get; set; }

        /// <summary>
        /// 测试类别
        /// </summary>
        public int TestType { get; set; }

        /// <summary>
        /// 测试工序
        /// </summary>
        public string TestProcess { get; set; }

        /// <summary>
        /// 测试项目
        /// </summary>
        public int TestItem { get; set; }

        /// <summary>
        /// 软件版本
        /// </summary>
        public string SoftVersion { get; set; }

        /// <summary>
        /// 员工编号
        /// </summary>
        public string UserNumber { get; set; }

        /// <summary>
        /// 工序名称
        /// </summary>
        public string ProcessName { get; set; }

        /// <summary>
        /// 设备编号
        /// </summary>
        public string MachineNo { get; set; }

        /// <summary>
        /// 服务器名称
        /// </summary>
        public string ServerName { get; set; }

        /// <summary>
        /// 服务器地址
        /// </summary>
        public string ServerHost { get; set; }

        [JsonIgnore]
        MachineDataCollectRequest Request { get; set; }
        #endregion

        public SunWayMes ()
        {
            Request = new MachineDataCollectRequest();
        }

        /// <summary>
        /// 产品过站检
        /// </summary>
        /// <param name="sn"></param>
        /// <returns></returns>
        public (bool, string, string) DataInspection (string sn)
        {
            return JudgeResult(Request.TestDataInspection(ProductName, WorkshopName, MOName, LineNumber, Station, MachineNo, TestType, TestProcess, ProcessName, TestItem, sn, ServerHost, ServerName));
        }

        /// <summary>
        /// 数据上传
        /// </summary>
        /// <param name="sn">产品SN</param>
        /// <param name="testData">测试数据</param>
        /// <param name="result">测试结果</param>
        /// <param name="testStartTime">测试开始时间</param>
        /// <param name="testEndTime">测试结束时间</param>
        /// <param name="parm1">参数1(如果有)</param>
        /// <param name="parm2">参数2(如果有)</param>
        /// <param name="parm3">参数3(如果有)</param>
        /// <returns></returns>
        public (bool, string, string) DataUpload (string sn, string testData, string result, string testStartTime, string testEndTime, string parm1 = "", string parm2 = "", string parm3 = "")
        {
            return JudgeResult(Request.MachineDataUpload(ProductName, WorkshopName, MOName, UserNumber, sn, LineNumber, Station, MachineNo, TestProcess, ProcessName, TestType, TestItem, testData, result, SoftVersion, testStartTime, testEndTime, parm1, parm2, parm3, ServerHost, ServerName));
        }

        /// <summary>
        /// 看板数据上传
        /// </summary>
        /// <param name="jsonData">每两小时信息</param>
        /// <param name="parm1">参数1(如果有)</param>
        /// <param name="parm2">参数2(如果有)</param>
        /// <param name="parm3">参数3(如果有)</param>
        /// <returns></returns>
        public (bool, string, string) YieldDataUpload (string jsonData, string parm1 = "", string parm2 = "", string parm3 = "")
        {
            var time = DateTime.Now;
            return JudgeResult(Request.ProductYieldDataUpload(ProductName, WorkshopName, MOName, UserNumber, LineNumber, Station, MachineNo, TestProcess, ProcessName, TestType, TestItem, time.ToString("yyyy-MM-dd HH:mm:ss"), time.ToString("yyyy-MM-dd"), jsonData, parm1, parm2, parm3, ServerHost, ServerName));
        }

        /// <summary>
        /// 设备状态信息上传
        /// </summary>
        /// <param name="statusCode">运行状态</param>
        /// <param name="uploadTime">触发时间</param>
        /// <param name="recoveryTime">恢复时间(报警时填写)</param>
        /// <param name="alarmCode">报警代码(报警时填写)</param>
        /// <returns></returns>
        public (bool, string, string) StatusDataUpload (STATUSCODE statusCode, string uploadTime, string recoveryTime = "", string alarmCode = "")
        {
            var time = DateTime.Now;
            return JudgeResult(Request.MachineStatusDataUpload(ProductName, WorkshopName, MOName, LineNumber, Station, MachineNo, TestType, TestProcess, ProcessName, TestItem, ((int)statusCode).ToString(), alarmCode, uploadTime, recoveryTime, ServerHost, ServerName));
        }

        protected (bool, string, string) JudgeResult (string str)
        {
            if(string.IsNullOrEmpty(str))
            {
                return (false, "未收到MES回复", "检查网络连接");
            }
            var json = JsonConvert.DeserializeObject<Dictionary<string, string>>(str);
            return (json["StatusCode"] == "Success", json["Message"], ErrInfo(json["Data"]));
        }

        protected string ErrInfo (string err)
        {
            switch(err)
            {
                case "ArgumentNullError":
                    return $"[{err}]接口传参数出现空值错误,需要检查输入的参数";
                case "ProductionError":
                    return $"[{err}]产品批次或产品信息错误,检查生产相关内容";
                case "FAIError":
                    return $"[{err}]请检查 AFMT 的尺寸,FAI 不在标准范围内";
                case "TestError":
                    return $"[{err}]找不到产品测试信息或者测试结果 NG";
                case "SystemError":
                    return $"[{err}]系统相关问题报错,请联系 MES IT";
                case "ScanError":
                    return $"[{err}]针对扫描的条码发生重复,检查是否重码";
                case "PackingError":
                    return $"[{err}]扫满后还持续扫描引发的错误";
                case "ParameterError":
                    return $"[{err}]输入的参数有误";
                case "MissingError":
                    return $"[{err}]缺少关键内容错误";
                case "ValidationError":
                    return $"[{err}]输入内容未通过验证";
                case "NetworkError":
                    return $"[{err}]无法连接到 MES 数据库,需要检查网络";
                case "ProcedureError":
                    return $"[{err}]存储过程错误";
                case "OtherError":
                    return $"[{err}]其他类型错误";
                default:
                    return "";
            }
        }
    }
}
